package com.eastfair.notice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.notice.dto.ImDialogSaveDTO;
import com.eastfair.notice.entity.ImDialog;
import com.eastfair.notice.service.ImDialogService;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.notice.dto.ImDialogUpdateDTO;
import com.eastfair.notice.dto.ImDialogPageQuery;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * Im对话列表
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/imDialog")
@Api(value = "ImDialog", tags = "Im对话列表")
public class ImDialogController extends SuperController<ImDialogService, Long, ImDialog, ImDialogPageQuery, ImDialogSaveDTO, ImDialogUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ImDialog> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ImDialog> imDialogList = list.stream().map((map) -> {
            ImDialog imDialog = ImDialog.builder().build();
            //TODO 请在这里完成转换
            return imDialog;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(imDialogList));
    }

    /**
     * 查询对话列表
     * @param query fromUserId 查询人的userAccountId
     * @return list
     */
    @ApiOperation(value = "查询对话列表",notes = "查询对话列表")
    @Override
    public R<List<ImDialog>> query(@RequestBody ImDialog query) {
        QueryWrapper<ImDialog> queryWrapper = new QueryWrapper<>();
        //传入ID为发出人or接收人
        queryWrapper.lambda()
                .eq(ImDialog::getProjectId, ContextUtil.getProjectId())
                .and(wrapper -> wrapper.eq(ImDialog::getFromUserId,query.getFromUserId())
                        .or()
                        .eq(ImDialog::getToUserId,query.getFromUserId()));
        return success(baseService.list(queryWrapper));
    }
}
